<%@ page language="java"  pageEncoding="UTF-8"%>
<%@ page import="net.ssta.util.bill.UserPoint" %>
<%@ page import="net.ssta.util.bill.UserBill" %>
<%@ page import="net.ssta.util.bill.UserBillException" %>
<%@ page import="com.yztc.rc.api.framework.entity.UserAccount" %>
<%@page import="net.ssta.util.bill.chargeobj.KjcgCharge"%>
<%@page import="net.ssta.util.bill.chargeobj.ChargeInterface"%>
<%@ page import="com.yztc.rc.api.framework.ContextFactory" %>
<%@ page import="com.yztc.rc.api.framework.Context" %>
<%@ page import="java.util.Date" %>
<%
String kjcgId=request.getParameter("kjcgid");
ChargeInterface charge=new KjcgCharge(kjcgId,title);
int cost=charge.calCostNumber();


String userId=net.ssta.common.LoginUser.getUserId();
boolean hasReaded=false;
String userType=net.ssta.common.LoginUser.getUserType();

 
if(cost!=0&&!"0".equals(userType)){//不用扣费，或者用户是内部用户
  
	
  String ensure=request.getParameter("ensure");
  Context context1= ContextFactory.getContext();
  UserAccount ua1=null;
  if(context1!=null){
  	 ua1 =context1.getUserAccount();
  }
  if(ua1==null){
	  //如果没有登录，则跳到登录页面
	%>
	<script>
	   window.location.href='/rcsso/login?service='+encodeURIComponent(window.location.href);
	</script>
	<% 
		return;
	}
   
  UserPoint userp=new UserPoint(userId);
  boolean isvip=userp.isVip();
  boolean isInIpRange=UserPoint.isInIpRange(userId,request);
 
  if(isvip&&isInIpRange){
	  
  }else if(isvip&&!isInIpRange){
 %>
	  
	  <div id="main_login_top"></div>
				<div id="main_login_main" style="margin-top:40px;">
				    <div id="main_login_main_left">
				    <table><tr><td><IMG src="/SSTA/common/images/info.png" width="64" height="64" /> 	 </td>
				    <td style="font-size:14px;line-height:25px;color:#0054A5;">
				    对不起，你的是<%=request.getRemoteAddr() %>，不在允许的范围内，若有疑问，请联系管理员
				      </td></tr></table>
				</div>
	  <% 
	  return;
	  
  }else{ 
	  
	  
	  
  
  
	  hasReaded=UserPoint.hasReadInfo(userId,kjcgId);
	  if(ensure==null){
	  		 
			  StringBuffer   url1   =   request.getRequestURL();   
			  if   (request.getQueryString()   !=   null)   {   
			     url1.append("?");   
			    url1.append(request.getQueryString());   
			  }  
			 
			  userId=ua1.getUserId();
			  
			 if(!hasReaded){
	  %>
	  
	  
	<div id="main_login_top"></div>
	<div id="main_login_main" style="margin-top:40px;">
	    <div id="main_login_main_left">
	    <table><tr><td><IMG src="/SSTA/common/images/info.png" width="64" height="64"> 	 </td>
	    <td style="font-size:14px;line-height:25px;color:#0054A5;">
	    本信息扣除单价为：<font color="red"><%=cost%></font>,<br/>
	    点击“<A href='<%=url1+"&ensure=1"%>' style="color:blue;text-decoration:underline;">确定</A>”继续，
	    点击“<A href="javascript:window.close();" style="color:blue;text-decoration:underline;">取消</A>”关闭<br/>
	      </td></tr></table>
	</div>
		 
	  <%
	  return;
	  }
	  }
	
			 
		if(!hasReaded){
			String message=null;
			//如果没有阅读过，则扣费
			userId=ua1.getUserId();
			UserBill bill=new UserBill();
			bill.initBillByRequest(request);
		  try{
				new UserPoint(userId).charge(charge,bill);
			}catch(UserBillException e){
				if(UserBillException.pointNotEnough.equals(e.getErrorCode())){
					message="用户积分不足，请充值";
					
				}
				else{
					message="扣费出错，请重试";
				}
			}catch(Exception e){
				message="扣费出错，请重试";
			} 
			
			if(message!=null){
			%>
			<div id="main_login_top"></div>
				<div id="main_login_main" style="margin-top:40px;">
				    <div id="main_login_main_left">
				    <table><tr><td><IMG src="/SSTA/common/images/info.png" width="64" height="64" /> 	 </td>
				    <td style="font-size:14px;line-height:25px;color:#0054A5;">
				    <%=message %>
				      </td></tr></table>
				</div>
	<%		
			return;
		}
		}
  }	
}
%>
 
